package org.daidai.abstrusewriting.homepage.hot.adapters;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.homepage.hot.activity.EditRecommendActivity;
import org.daidai.abstrusewriting.homepage.hot.model.HotRecommends;
import org.daidai.abstrusewriting.homepage.hot.model.Item;

import java.util.List;

/**
 * Created by Administrator on 2016/11/2.
 */

public class MyHotItemAdapter extends BaseAdapter {
    private List<HotRecommends.HotItem> mList;
    private Context mContext;

    public MyHotItemAdapter(Context context, List<HotRecommends.HotItem> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return mList.get(i).getCategoryId();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_item_common, viewGroup, false);
            view.setTag(new ViewHolder(view));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        HotRecommends.HotItem hotItem = mList.get(i);
        holder.allTitle.setText(hotItem.getTitle());
        List<Item> items = hotItem.getItems();
        if (items.size() >= 3) {
            Item item = items.get(0);
            holder.mTitle0.setText(item.getTrackTitle());
            holder.tip0.setText(item.getTitle());
            final ImageView imageView0 = holder.mImageView0;
            Picasso.with(imageView0.getContext())
                    .load(item.getCoverMiddle())
                    .config(Bitmap.Config.ARGB_8888)
                    .into(imageView0);
            imageView0.setTag(item);
            imageView0.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Object tag = v.getTag();
                    if (tag instanceof Item) {
                        Item item = (Item) tag;
                        Intent intent = new Intent(mContext, EditRecommendActivity.class);
                        intent.putExtra("data",item);
                        mContext.startActivity(intent);
                    }
                }
            });

            Item item1 = items.get(1);
            holder.mTitle1.setText(item1.getTrackTitle());
            holder.tip1.setText(item1.getTitle());
            ImageView imageView1 = holder.mImageView1;
            Picasso.with(imageView1.getContext())
                    .load(item1.getCoverMiddle())
                    .config(Bitmap.Config.ARGB_8888)
                    .into(imageView1);
            imageView1.setTag(item1);
            imageView1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Object tag = v.getTag();
                    if (tag instanceof Item) {
                        Item item = (Item) tag;
                        Intent intent = new Intent(mContext, EditRecommendActivity.class);
                        intent.putExtra("data",item);
                        mContext.startActivity(intent);
                    }
                }
            });

           Item item2 = items.get(2);
            holder.mTitle2.setText(item2.getTrackTitle());
            holder.tip2.setText(item2.getTitle());
            ImageView imageView2 = holder.mImageView2;
            Picasso.with(imageView2.getContext())
                    .load(item2.getCoverMiddle())
                    .config(Bitmap.Config.ARGB_8888)
                    .into(imageView2);
            imageView2.setTag(item2);
            imageView2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Object tag = v.getTag();
                    if (tag instanceof Item) {
                        Item item = (Item) tag;
                        Intent intent = new Intent(mContext, EditRecommendActivity.class);
                        intent.putExtra("data",item);
                        mContext.startActivity(intent);
                    }
                }
            });

        }
        return view;
    }


    public static class ViewHolder {
        private TextView allTitle;
        private ImageView mImageView0;
        private TextView mTitle0;
        private TextView tip0;
        private ImageView mImageView1;
        private TextView mTitle1;
        private TextView tip1;
        private ImageView mImageView2;
        private TextView mTitle2;
        private TextView tip2;

        public ViewHolder(View view) {
            allTitle = (TextView) view.findViewById(R.id.item_common_title);
            mImageView0 = ((ImageView) view.findViewById(R.id.item_common_imageview0));
            tip0 = ((TextView) view.findViewById(R.id.item_common_tip0));
            mTitle0 = ((TextView) view.findViewById(R.id.item_common_title0));
            mImageView1 = ((ImageView) view.findViewById(R.id.item_common_imageview1));
            tip1 = ((TextView) view.findViewById(R.id.item_common_tip1));
            mTitle1 = ((TextView) view.findViewById(R.id.item_common_title1));
            mImageView2 = ((ImageView) view.findViewById(R.id.item_common_imageview2));
            tip2 = ((TextView) view.findViewById(R.id.item_common_tip2));
            mTitle2 = ((TextView) view.findViewById(R.id.item_common_title2));
        }
    }
}
